{{scroll-tracker name="discourse-automation-edit"}}

<section class="discourse-automation-form edit">
  <form class="form-horizontal">
    {{form-error error=error}}

    <section class="form-section edit">
      <div class="control-group">
        <label class="control-label">
          {{i18n "discourse_automation.models.automation.name.label"}}
        </label>

        <div class="controls">
          {{text-field
            value=automationForm.name
            type="text"
            classNames="input-large"
            autofocus="autofocus"
          }}
        </div>
      </div>

      <div class="control-group automation-enabled">
        <label class="control-label {{unless automationForm.enabled "disabled"}}">
          {{i18n "discourse_automation.models.automation.enabled.label"}}
        </label>

        <div class="controls">
          {{input
            type="checkbox"
            checked=automationForm.enabled
            click=(action (mut automationForm.enabled) value="target.checked")
          }}
        </div>
      </div>

      <div class="control-group">
        <label class="control-label">
          {{i18n "discourse_automation.models.script.name.label"}}
        </label>

        <div class="controls">
          {{combo-box
            class="scriptables"
            value=automationForm.script
            content=model.scriptables
            onChange=(action "onChangeScript")
            options=(hash
              filterable=true
            )
          }}
        </div>
      </div>
    </section>

    <section class="trigger-section form-section edit">
      <h2 class="title">
        {{i18n "discourse_automation.edit_automation.trigger_section.title"}}
      </h2>

      <div class="control-group">

        {{#if model.automation.script.forced_triggerable}}
          <div class="alert alert-warning">
            {{i18n "discourse_automation.edit_automation.trigger_section.forced"}}
          </div>
        {{/if}}

        <label class="control-label">
          {{i18n "discourse_automation.models.trigger.name.label"}}
        </label>

        <div class="controls">
          {{combo-box
            class="triggerables"
            value=automationForm.trigger
            content=model.triggerables
            onChange=(action "onChangeTrigger")
            options=(hash
              filterable=true
              none="discourse_automation.select_trigger"
              disabled=model.automation.script.forced_triggerable
            )
          }}
        </div>
      </div>

      {{#if automationForm.trigger}}
        {{#if model.automation.trigger.doc}}
          <div class="alert alert-info">
            <p>{{model.automation.trigger.doc}}</p>
          </div>
        {{/if}}

        {{#if model.automation.enabled}}
          {{#if model.automation.next_pending_automation_at}}
            <div class="alert alert-info next-trigger">
              <p>{{i18n "discourse_automation.edit_automation.trigger_section.next_pending_automation" date=nextPendingAutomationAtFormatted}}</p>

              {{d-button
                label="discourse_automation.edit_automation.trigger_section.trigger_now"
                class="btn-primary trigger-now-btn"
                isLoading=isTriggeringAutomation
                action=(action "onManualAutomationTrigger" model.automation.id)
              }}
            </div>
          {{/if}}
        {{/if}}

        {{#each triggerFields as |field|}}
          {{automation-field
            automation=automation
            field=field
            onChangeField=(action "onChangeField")
            saveAutomation=(action "saveAutomation" automation)
          }}
        {{/each}}
      {{/if}}
    </section>

    {{#if automationForm.trigger}}
      {{#if scriptFields}}
        <section class="fields-section form-section edit">
          <h2 class="title">
            {{i18n "discourse_automation.edit_automation.fields_section.title"}}
          </h2>

          {{#if model.automation.script.with_trigger_doc}}
            <div class="alert alert-info">
              <p>{{model.automation.script.with_trigger_doc}}</p>
            </div>
          {{/if}}

          <div class="control-group">
            {{#each scriptFields as |field|}}
              {{automation-field
                automation=automation
                field=field
                onChangeField=(action "onChangeField")
                saveAutomation=(action "saveAutomation" automation)
              }}
            {{/each}}
          </div>
        </section>
      {{/if}}

      <div class="control-group">
        {{d-button
          isLoading=isUpdatingAutomation
          class="btn-primary"
          label="discourse_automation.update"
          type="submit"
          action=(action "saveAutomation" automation)
        }}
      </div>
    {{/if}}
  </form>
</section>
